# -*-coding:utf-8-*-
from utils import read_txt,cut_sent,read_hed,read_word
from Syntactic_Analysis import ParseResult
import regex
'''
0 主||主+名词/名词词组/数量词组 
1 主||主+形容词/形容词词组 
2 主||主+动词/动词词组
3 主||主\主+谓
4 状+主||主+谓
5 主||状+主+谓
6 主||主+状+否定形式的谓
7 主||“不/没（有）”+主+谓

4 状+主||主+谓
6 主||主+状+否定形式的谓
7 主||“不/没（有）”+主+谓
5 主||状+主+谓
1 主||主+形容词/形容词词组 
2 主||主+动词/动词词组
0 主||主+名词/名词词组/数量词组 
3 主||主\主+谓
'''


'''
1. 含VOB和SBV则判断为含主谓成分 （一般两个主语分别被识别为SBV和VOB）
2. 判别顺序：
    4 状+主||主+谓  
    6 主||主+状+否定形式的谓
    7 主||“不/没（有）”+主+谓
    5 主||状+主+谓
    1 主||主+形容词/形容词词组 
    2 主||主+动词/动词词组
    0 主||主+名词/名词词组/数量词组 
    3 主||主\主+谓
3. 存下最前面的和最后面的SBV或VOB作为主语起始位置
4. 状语（ADV）在开始主语前判为 -> 4
   “不/没（有）”在开始位置后，结束位置前判为 -> 7
   .......
   .......
'''

def judge_type(hed_info, modifi_info, fenci_res, pos_res):
    adv_info = modifi_info['adv_info']  # ADV信息
    mt_info = modifi_info['mt_info']    # MT信息
    sbv_info = modifi_info['sbv_info']  # SBV成分
    cmp_info = modifi_info['cmp_info']  # CMP成分
    att_info = modifi_info['att_info']
    vob_info = modifi_info['vob_info']
    deprel = modifi_info['dep_rel']
    sbv_h = read_hed(sbv_info,hed_info['word'])
    noun_lst = ['n', 'f', 's', 't', 'nr', 'ns', 'nt', 'nw', 'nz', 'r', 'an', 'vn', 'PER', 'LOC', 'TIME']
    verb_lst = ['v', 'vd', 'vn']
    adj_lst = ['a', 'ad', 'an']

    type_lst = ['主||主+名词/名词词组/数量词组', '主||主+形容词/形容词词组', '主||主+动词/动词词组', '主||主\主+谓',
                '状+主||主+谓', '主||状+主+谓', '主||主+状+否定形式的谓', '主||“不/没（有）”+主+谓', '其他句型']

    type_id = len(type_lst) - 1
    zhu_num = []
    if (sbv_info != [] and vob_info != []) or (
        sbv_h and sbv_h['pos'] in noun_lst
        and [att for att in att_info if att['pos'] in noun_lst and att['num'] < sbv_h['num']]):
        print("含有主谓关系")
        for i in range(len(deprel)):
            if deprel[i] == 'SBV' or deprel[i] == 'VOB':
                zhu_num.append(i)
        zhu_startnum = zhu_num[0]
        zhu_endnum = zhu_num[-1]
        print("zhu_startnum", zhu_startnum,zhu_endnum)
        print("deprel", deprel)
        for j in range(len(pos_res)):
            if deprel[j] == 'ADV' and j < zhu_startnum:
                type_id = 4
                return type_lst[type_id]
            # if
            if fenci_res[j] in ['不', '没', '没有'] and zhu_startnum < j < zhu_endnum:
                type_id = 7
                return type_lst[type_id]
            if deprel[j] == 'ADV' and zhu_startnum < j < zhu_endnum:
                type_id = 5
                return type_lst[type_id]
            if pos_res[j] in adj_lst and j > zhu_endnum:
                type_id = 1
                return type_lst[type_id]
            if pos_res[j] in verb_lst and j > zhu_endnum:
                type_id = 2
                return type_lst[type_id]
            if pos_res[j] in noun_lst and j > zhu_endnum:
                type_id = 0
                return type_lst[type_id]
    else:
        print("不是主谓谓语句")
            # else:
            #     type_id = len(type_lst) - 1
            #     return type_lst[type_id]
    # else:
    #     type_id = len(type_lst) - 1
    #     return type_lst[type_id]

    return type_lst[type_id]


if __name__ == '__main__':
    # path = './data/test.txt'  # 读取数据
    # path = './data/test_adjpre.txt'  # 读取数据
    path = './data/test_zhuwei.txt'  # 读取数据
    sentences = read_txt(path)
    for sent in sentences:
        print('*' * 125)
        fenci_res, pos_res, ddp_res = cut_sent(sent)
        print('分词：', fenci_res)
        print('词性：', pos_res)
        print('句法：', ddp_res)

        # del pos_res[sbvn]
        # del ddp_res[0]['word'][sbvn], ddp_res[0]['head'][sbvn], ddp_res[0]['deprel'][sbvn]


        pr = ParseResult(fenci_res, pos_res, ddp_res)
        modifi_info = pr.get_modifi_info()
        hed_info = pr.get_hed_info()
        print(judge_type(hed_info, modifi_info, fenci_res, pos_res))



